using UnityEngine;
using System.Collections;

public class ForgeRobotComponents : BuildingOperationJob {
	
	Bar bar;
	Metal material;
	int ftype; //0 = head, 1 = arms, 2 = legs, 3 = torso, 4 = tank, 5 = mining tool, 6 = construction tool, 7 = deconstruction tool
	
	bool hauled;
	bool haulingStarted;
	
	HaulingJob h;
	
	public ForgeRobotComponents(Bar nbar, int nx, int ny, int nz, int nftype):base(nx, ny, nz){
		bar = nbar;
		material = (Metal)bar.getMaterial();
		
		ftype = nftype;
		if (ftype < 0 | ftype > 7){
			ftype = 0;
		}
		
		jobType = 14;
		
		hauled = false;
		haulingStarted = false;
		
		h = new HaulingJob(bar, x, y, z, this);
		
		if ((material.GetType()).ToString() == "Aluminum"){
			ItemList.aluminumBars.Remove(bar);
		} else if ((material.GetType()).ToString() == "Copper"){
			ItemList.copperBars.Remove(bar);
		} else if ((material.GetType()).ToString() == "Iron"){
			ItemList.ironBars.Remove(bar);
		} else if ((material.GetType()).ToString() == "Steel"){
			ItemList.steelBars.Remove(bar);
		} else if ((material.GetType()).ToString() == "GalvanizedSteel"){
			ItemList.galvanizedSteelBars.Remove(bar);
		} else if ((material.GetType()).ToString() == "Brass"){
			ItemList.brassBars.Remove(bar);
		} else if ((material.GetType()).ToString() == "Bronze"){
			ItemList.bronzeBars.Remove(bar);
		} else {
			Debug.LogError (material.GetType());
		}
		
		if (ftype == 0){
			jobLabel = "Forge Head";
		} else if (ftype == 1){
			jobLabel = "Forge Arms";
		} else if (ftype == 2){
			jobLabel = "Forge Legs";
		} else if (ftype == 3){
			jobLabel = "Forge Torso";
		} else if (ftype == 4){
			jobLabel = "Forge Tank";
		} else if (ftype == 5){
			jobLabel = "Forge Mining Tool";
		} else if (ftype == 6){
			jobLabel = "Forge Construction Tool";
		} else if (ftype == 7){
			jobLabel = "Forge Deconstruction Tool";
		}
		
	}
	
	public bool workOn(double workspeed, Robot robot){
		bool isDone = false;
		try {
			hauled = h.j.complete;
		} catch {
			hauled = false;
		}
		if (hauled){
			isDone =  base.workOn(workspeed, robot);
		} else {
			if (!haulingStarted){
				robot.assignJob(h, true);
				haulingStarted = true;
				Debug.Log ("Sure thing");
			}
		}
		if (isDone){
			
			GameObject.Destroy(bar.item);
			
			if (ftype == 0){
				Head item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("HeadPrefab"));
				item = (Head)obj.GetComponent("Head");
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x, y + 5, z + 5);
			} else if (ftype == 1){
				Arms item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("ArmsPrefab"));
				item = ((Arms)obj.GetComponent("Arms"));
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x, y + 5, z + 5);
			} else if (ftype == 2){
				Legs item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("LegsPrefab"));
				item = ((Legs)obj.GetComponent("Legs"));
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x, y + 5, z + 5);
			} else if (ftype == 3){
				Torso item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("TorsoPrefab"));
				item = ((Torso)obj.GetComponent("Torso"));
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x, y + 5, z + 5);
			} else if (ftype == 4){
				Tank item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("TankPrefab"));
				item = ((Tank)obj.GetComponent("Tank"));
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x + 5, y + 5, z);
			} else if (ftype == 5){
				MiningTool item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("MiningToolPrefab"));
				item = ((MiningTool)obj.GetComponent("MiningTool"));
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x + 10, y + 5, z);
			} else if (ftype == 6){
				ConstructionTool item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("ConstructionToolPrefab"));
				item = ((ConstructionTool)obj.GetComponent("ConstructionTool"));
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x, y + 5, z + 5);
			} else if (ftype == 7){
				DeconstructionTool item;
				GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("DeconstructionToolPrefab"));
				item = ((DeconstructionTool)obj.GetComponent("DeconstructionTool"));
				item.item = obj;
				item.starterate(material);
				obj.transform.position = new Vector3(x, y + 5, z + 5);
			}
			
			
			
			
		}
		return isDone;
	}
	
	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
